package com.platform.controller.platform;


import com.platform.common.annotation.Log;
import com.platform.common.core.domain.AjaxResult;
import com.platform.common.core.page.TableDataInfo;
import com.platform.common.enums.BusinessType;
import com.platform.common.enums.ResultEnum;
import com.platform.common.exception.BusinessException;
import com.platform.system.domain.dto.device.DeviceInfoDto;
import com.platform.system.domain.vo.device.DeviceInfoVo;
import com.platform.system.logic.DeviceInfoLogic;
import com.platform.system.service.ISysDeviceInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author Huangkai
 */
@Slf4j
@RestController
@RequestMapping("/device/deviceInfo")
public class DeviceInfoController {


    @Resource
    ISysDeviceInfoService deviceInfoService;

    @Resource
    DeviceInfoLogic deviceInfoLogic;

    /**
     * 查询日志
     */
    @Log(title = "查询设备", businessType = BusinessType.OTHER)
    @GetMapping("/selectDeviceInfoList")
    public TableDataInfo selectDeviceInfoList(@Validated DeviceInfoVo deviceInfoVo) {
        return deviceInfoService.selectDeviceInfoList(deviceInfoVo);
    }

    /**
     * 查询日志
     */
    @RequestMapping(value = "/aiVerity", method = {RequestMethod.GET,RequestMethod.POST})
    @ResponseBody
    public String selectDeviceInfoList(HttpServletRequest request) {
        return deviceInfoService.aiuiOperation(request);
    }

    /**
     * 获取在线设备
     */
    @Log(title = "查询设备", businessType = BusinessType.OTHER)
    @GetMapping("/getDeviceOnLineCount")
    public AjaxResult getDeviceOnLineCount() {
        Long count = deviceInfoService.getDeviceOnLineCount();
        return AjaxResult.success(count);
    }

    @Log(title = "激活设备", businessType = BusinessType.OTHER)
    @PostMapping("/activateDevice")
    public AjaxResult activateDevice(@RequestBody @Validated DeviceInfoVo deviceInfoVo) {
        log.info("激活设备{}", deviceInfoVo);
        return deviceInfoService.activateDevice(deviceInfoVo);
    }

    @Log(title = "获取设备详情", businessType = BusinessType.OTHER)
    @PostMapping("/selectDeviceInfoByMac")
    public AjaxResult selectDeviceInfoByMac(@RequestBody @Validated DeviceInfoVo deviceInfoVo) {
        DeviceInfoDto deviceInfoDto = deviceInfoService.selectDeviceInfoByMac(deviceInfoVo);
        if(deviceInfoDto == null){
            throw new BusinessException(ResultEnum.DATA_NOT_EXIST);
        }
        deviceInfoLogic.buildDeviceInfoDetail(deviceInfoDto);
        return AjaxResult.success(deviceInfoDto);
    }

    /**
     * 新增
     */
    @Log(title = "新增设备日志", businessType = BusinessType.INSERT)
    @PostMapping("/insertDeviceInfo")
    public AjaxResult insertDeviceInfo(@RequestBody @Validated DeviceInfoVo deviceInfoVo) {
        return deviceInfoService.insertDeviceInfo(deviceInfoVo);
    }

    /**
     * 修改状态
     */
    @Log(title = "修改日志上报状态", businessType = BusinessType.UPDATE)
    @PostMapping("/updateDeviceInfoLogStatus")
    public AjaxResult updateDeviceInfoLogStatus(@RequestBody @Validated DeviceInfoVo deviceInfoVo) {
        log.info("修改状态{}", deviceInfoVo);
        return deviceInfoService.updateDeviceInfoLogStatus(deviceInfoVo);
    }

    /**
     * 详情
     */
    @Log(title = "详情", businessType = BusinessType.OTHER)
    @GetMapping("/selectDeviceInfo/{uid}")
    public AjaxResult selectDeviceInfo(@PathVariable("uid") String uid) {
        DeviceInfoDto deviceInfoDto = deviceInfoService.selectDeviceInfo(uid);
        if(deviceInfoDto == null){
            throw new BusinessException(ResultEnum.DATA_NOT_EXIST);
        }
        deviceInfoLogic.buildDeviceInfo(deviceInfoDto);
        return AjaxResult.success(deviceInfoDto);
    }
    /**
     * 获取设备导入excel模板
     */
    @Log(title = "获取设备导入excel模板", businessType = BusinessType.OTHER)
    @PostMapping("/getImportTemplate")
    public void getImportTemplate(HttpServletResponse response) {
        deviceInfoService.getImportTemplate(response);
    }

    /**
     * 导入数据
     */
    @Log(title = "导入数据", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        return deviceInfoService.importData(file);
    }

    /**
     * 修改
     */
    @Log(title = "修改", businessType = BusinessType.UPDATE)
    @PostMapping("/updateDeviceInfo")
    public AjaxResult updateDeviceInfo(@RequestBody @Validated DeviceInfoVo deviceInfoVo) {
        return deviceInfoService.updateDeviceInfo(deviceInfoVo);
    }

    /**
     * 删除
     */
    @Log(title = "删除", businessType = BusinessType.DELETE)
    @PostMapping("/deleteDeviceInfoList")
    public AjaxResult deleteDeviceInfoList(@RequestBody @Validated DeviceInfoVo deviceInfoVo) {
        return deviceInfoService.deleteDeviceInfoList(deviceInfoVo);
    }
}
